package cn.doitedu.kafka.serde;

import io.protostuff.LinkedBuffer;
import io.protostuff.ProtobufIOUtil;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2020/11/11
 **/
public class ProtoDemo {
    public static void main(String[] args) throws Exception {
        Stu stu = new Stu("zhangsan", "beijing", 18);

        Schema<Stu> schema = RuntimeSchema.getSchema(Stu.class);

        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);


        byte[] bytes = ProtostuffIOUtil.toByteArray(stu, schema, buffer);


        FileOutputStream fo = new FileOutputStream("d:/pro.bytes");
        fo.write(bytes);

        fo.close();

        FileInputStream fi = new FileInputStream("d:/pro.bytes");
        Stu stu1 = new Stu();
        ProtostuffIOUtil.mergeFrom(fi,stu1,schema);

        System.out.println(stu1.getName());
        System.out.println(stu1.getAge());
        System.out.println(stu1.getAddr());

        fi.close();

    }
}
